package com.zhenwei.zscm.repo;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.ShippingViewEntity;
import com.zhenwei.zscm.mapper.IShippingViewMapper;
import com.zhenwei.zscm.mappings.ShippingViewMapping;
import com.zhenwei.zscm.model.dto.shipping.PageShippingDTO;
import com.zhenwei.zscm.model.vo.shipping.QueryShippingVO;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@Service
public class ShippingViewRepo extends ServiceImpl<IShippingViewMapper, ShippingViewEntity> {
    public List<PageShippingDTO> page(QueryShippingVO query) {
        var q = new LambdaQueryWrapper<ShippingViewEntity>();
        q.like(StrUtil.isNotBlank(query.getOrderNumber()), ShippingViewEntity::getOrderNumber, query.getOrderNumber());
        q.like(StrUtil.isNotBlank(query.getPoNumber()), ShippingViewEntity::getPoNumber, query.getPoNumber());
        q.like(StrUtil.isNotBlank(query.getContactor()), ShippingViewEntity::getContactor, query.getContactor());
        q.like(StrUtil.isNotBlank(query.getTelephone()), ShippingViewEntity::getTelephone, query.getTelephone());
        q.like(StrUtil.isNotBlank(query.getAddress()), ShippingViewEntity::getAddress, query.getAddress());
        q.like(StrUtil.isNotBlank(query.getLogisticsNumber()), ShippingViewEntity::getLogisticsNumber, query.getLogisticsNumber());
        q.like(StrUtil.isNotBlank(query.getSn()), ShippingViewEntity::getSn, query.getSn());
        q.eq(StrUtil.isNotBlank(query.getMaterialCode()), ShippingViewEntity::getMaterialCode, query.getMaterialCode());
        q.eq(StrUtil.isNotBlank(query.getPurchaseCompanyCode()), ShippingViewEntity::getPurchaseCompanyCode, query.getPurchaseCompanyCode());
        q.ge(query.getCreateTimeStart() != null, ShippingViewEntity::getCTime, query.getCreateTimeStart());
        q.le(query.getCreateTimeEnd() != null, ShippingViewEntity::getCTime, query.getCreateTimeEnd());
        q.ge(query.getShippingDateStart() != null, ShippingViewEntity::getShippingDate, query.getShippingDateStart());
        q.le(query.getShippingDateEnd() != null, ShippingViewEntity::getShippingDate, query.getShippingDateEnd());

        q.select(ShippingViewEntity::getGuid,
                ShippingViewEntity::getOrderNumber,
                ShippingViewEntity::getPoNumber,
                ShippingViewEntity::getContactor,
                ShippingViewEntity::getTelephone,
                ShippingViewEntity::getAddress,
                ShippingViewEntity::getLogisticsCompanyCode,
                ShippingViewEntity::getLogisticsCompany,
                ShippingViewEntity::getLogisticsNumber,
                ShippingViewEntity::getPurchaseCompany,
                ShippingViewEntity::getPurchaseCompanyCode,
                ShippingViewEntity::getShippingDate,
                ShippingViewEntity::getCUser,
                ShippingViewEntity::getCTime,
                ShippingViewEntity::getMaterialCount);
        q.groupBy(ShippingViewEntity::getGuid,
                ShippingViewEntity::getOrderNumber,
                ShippingViewEntity::getPoNumber,
                ShippingViewEntity::getContactor,
                ShippingViewEntity::getTelephone,
                ShippingViewEntity::getAddress,
                ShippingViewEntity::getLogisticsCompanyCode,
                ShippingViewEntity::getLogisticsCompany,
                ShippingViewEntity::getLogisticsNumber,
                ShippingViewEntity::getPurchaseCompany,
                ShippingViewEntity::getPurchaseCompanyCode,
                ShippingViewEntity::getShippingDate,
                ShippingViewEntity::getCUser,
                ShippingViewEntity::getCTime,
                ShippingViewEntity::getMaterialCount);

        var p = new Page<ShippingViewEntity>(query.getPageNo(), query.getPageSize(), true);
        var page = this.page(p, q);
        query.setTotalCount(page.getTotal());
        List<ShippingViewEntity> list = page.getRecords();
        return Linq.mapList(list, ShippingViewMapping.INSTANCE::entity2Dto);
    }
    public Date getProductShippingDate(String productSn) {
        var q = new LambdaQueryWrapper<ShippingViewEntity>()
                .eq(ShippingViewEntity::getSn, productSn)
                .orderByDesc(ShippingViewEntity::getCTime);
        var list = this.list(q);
        return Linq.getFirst(list, ShippingViewEntity::getShippingDate);
    }
    public HashMap<String, Date> getProductShippingDate(List<String> productSn) {
        var q = new LambdaQueryWrapper<ShippingViewEntity>()
                .in(ShippingViewEntity::getSn, productSn)
                .orderByDesc(ShippingViewEntity::getCTime);
        var list = this.list(q);
        var result= new HashMap<String, Date>();

        list.forEach(e->{
            Optional<ShippingViewEntity> first = list.stream()
                    .filter(entity -> entity.getSn().equals(e.getSn()))
                    .max((prev, curr) -> DateUtil.compare(prev.getCTime(), curr.getCTime()));
            if(result.containsKey(e.getSn())){
               return;
            }
            result.put(e.getSn(), first.isPresent() ? first.get().getShippingDate() :  e.getShippingDate());
        });
        return result;
    }
}
